﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HR.Appointments.Service;
using HR.Appointments.Service.ValueObjects;

public partial class UserInfo : UserPage
{
    #region Private Static Members
    private static DateTime _bookingDate;
    private static TimeUnit _bookingTime;
    private static int _activityId;
    private static int _workstationId;
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        _imgConfirm.Visible = IsLoggedIn;
		if (!IsPostBack)
        {
            _lbtnCompanyBooking.Visible = IsLoggedIn;
            _activityId = PreviousPage.ActivityId;
            _workstationId = PreviousPage.WorkstationId;
            _bookingDate = PreviousPage.BookingDate;
            _bookingTime = PreviousPage.BookingTime;
        }
    }

    protected void _btnNewOrder_Click(object sender, EventArgs e)
    {
        var customerinfo = Customer.Create(
            _txtCustomerName.Text,
            _txtCustomerPhone.Text,
            _txtCustomerEmail.Text);

        var order = OrderEntity.Create(
            GetCompanyIdFromCookie(),
            DateTime.Now,
            _bookingDate,
            _bookingTime,
            0,
            customerinfo,
            _activityId,
            _workstationId);

        if (IsLoggedIn)
        {
            try
            {
                //book order
                Facade.Employee().BookOrder(order);

                //go to booking, do not display alert for logon users
                Server.Transfer("~/CompanyBooking.aspx",true);
            }
            catch (SubmitException)
            {
				//Display error message                                      
                Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að skrá pöntun.');", true);
                return;
            }
        }
        else
        {
            try
            {
                //place an unconfirmed order 
                Facade.Anonymous().PlaceOrder(order);

                //display response to user                    
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "ErrorAlert", "alert('Pöntun móttekin.  Staðfesting verður send með SMS.');location.replace('Company.aspx');", true);
            }
            catch (SubmitException)
            { 
                //Display error message
                Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að skrá pöntun.');", true);
                return;
            }
        }
    }
    #endregion

    #region Link Transfer
    protected void _ibmHomePage_Click(object sender, ImageClickEventArgs e)
	{
        if (!IsPostBack || (e.X != 0 && e.Y != 0))
        {
            Server.Transfer("~/Company.aspx",true);
        }
	}

    protected void _lbtnCompanyBooking_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/CompanyBooking.aspx");
    }
    #endregion
}
